﻿using AutoMapper;
using B.S.Material.Domain.Production_rejection;
using B.S.Material.ErrorCode;
using B.S.Material.Wirth.Api.Applications.Command.StrippingCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Wirth.Api.Applications.Handler.StrippingHandler
{
    public class AddStrippingHandler : IRequestHandler<AddStrippingCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddStrippingHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<ApiResult<object>> Handle(AddStrippingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnum.添加成功;
            result.MAG = ApiEnum.添加成功.ToString();
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "SCTL" + yeartime;
            var model = mapper.Map<Stripping>(request);
            model.IsDel = false;
            model.documentNumber = ProductCodess1;
            int Fee = await db.Insertable(model).ExecuteReturnIdentityAsync();

            result.data = new { Fee };

            return await Task.FromResult(result);
        }
    }
}
